<?php

require_once 'init.php';
require_once 'components/tours.php';

$smarty->assign('page',new Page('schedule'));

$page = new Page('schedule');
$smarty->assign('page',$page);

$where = array('TRUE');
if ($par->asInt('rout'))
{
	$rout = $par->asInt('rout');
	$where[] = "routs.id = $rout";
	$smarty->assign('routChosen',$rout);
}

if ($par->asInt('persons'))
{
	$persons = $par->asInt('persons');
	$where[] = "places_available >= $persons";
	$smarty->assign('persons',$persons);
}

if ($par->asInt('year'))
{
	$year = $par->asInt('year');
	$where[] = "(YEAR(start_date) = $year OR YEAR(end_date) = $year)";
	$smarty->assign('year',$year);
}

if ($par->asInt('month'))
{
	$month = $par->asInt('month');
	$where[] = "(MONTH(start_date) = $month OR MONTH(end_date) = $month)";	
	$smarty->assign('month',$month);
}


$smarty->assign('tours',
	query2arrays("
	 SELECT 
			tours_schedule.id, 
			DATE_FORMAT(start_date,'%d.%m.%Y') AS start_date, 
			DATE_FORMAT(end_date,'%d.%m.%Y') AS end_date,
			price, 
			places_available, 
			boats.id AS boat_id, 
			boats.name AS boat_name, 
			routs.id AS rout_id, 
			routs.name AS rout_name,
			DATEDIFF(tours_schedule.end_date,tours_schedule.start_date) AS duration_days,
			YEAR(start_date)=YEAR(end_date) AS same_year,
			MONTH(start_date) AS m,
			MONTHNAME(start_date) AS month_name
	 FROM tours_schedule
	 INNER JOIN boats ON tours_schedule.boat = boats.id
	 LEFT OUTER JOIN routs ON tours_schedule.rout = routs.id
	 WHERE ".implode(' AND ',$where)."
	 ORDER BY tours_schedule.start_date, tours_schedule.end_date
	")
);

	  

show(
	'tours', 
	$page->name()
);

?>